package com.neusoft.mall.model.dao;

import com.neusoft.mall.model.Product;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ProductDao {
    private List<Product> list = new ArrayList<>();

    public ProductDao() {
        list.add(new Product(1,"李宁裤子","服饰",
                new BigDecimal("300.001")));
        list.add(new Product(2,"李宁衣服","服饰",
                new BigDecimal("400.001")));
        list.add(new Product(3,"华为手机","电子",
                new BigDecimal("3000.001")));
        list.add(new Product(4,"小米汽车","交通工具",
                new BigDecimal("300000.001")));
    }

    public List<Product> queryAll(){
        return list;
    }

    public Product queryProById(Integer id){
        for (Product product : list) {
            if(product.getId() == id){
                return product;
            }
        }
        return null;
    }

    public List<Product> queryProByName(String name){
        List<Product> bList = new ArrayList<>();
        for (Product product : list) {
            if(product.getName().contains(name)){
                bList.add(product);
            }
        }
        return bList;
    }

    public Boolean delPro(Integer id){
        //productList.removeIf(val -> val.getId() == id);
        Iterator<Product> iterator = list.iterator();
        while(iterator.hasNext()){
            if(iterator.next().getId() == id) {
                iterator.remove();
                return true;
            }
        }
        return false;
    }

    public void addPro(Product product){
        list.add(product);
    }
}
